﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;
using MvcForum.Net.Business;

namespace MvcForum.Net
{
    /// <summary>
    /// Summary description for WebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    [ScriptService]
    public class WebService : System.Web.Services.WebService
    {

        private JavaScriptSerializer _serializer;

        #region validate
        private static bool IsPostedThisSite
        {
            get
            {
                if (HttpContext.Current.Request.UrlReferrer == null || HttpContext.Current.Request.UrlReferrer.Host != HttpContext.Current.Request.Url.Host)
                {

                    throw new HttpException("This form wasn’t submitted from this site!");

                }
                return true;
            }
        }

        #endregion

        [WebMethod]
        public void AddPost(string _topicName, int forumId, string _html, string pwd)
        {

            try
            {
                var forum = ForumsRepository.ForumDetail(forumId);

                string _username = "duong_ba_ho";
                int _userId = 4;
                int Id;
                //TopicsRepository.Add(null, _topicName, forumId, null, "", _html, null, 0,null, true, "", "", _username,_userId, "", out Id);

                //TopicsRepository.Add(0, _topicName, forumId, null, "", _html, 0, 0,
                //                                 DateTime.UtcNow, true, "", "", _username,
                //                                 _userId, "172.1.1.1", out Id);


                int priority=0;
                DateTime? priorityDate = null;
                string editedBy = string.Empty;
                string icon = "";
                bool isAprroved = true;
                string _remoteUrl = "";
                int? replyTo = null;

                TopicsRepository.Add(null, _topicName, forumId, null, icon, _html, replyTo, priority,
                                             priorityDate, isAprroved, editedBy, _remoteUrl, _username,
                                             _userId, "172.1.1.1", out Id);

                ForumsRepository.UpdateLastPost(forum.ParentId, 1, 0, _username, Id, _topicName);
            }
            catch (Exception e)
            {
                
                throw new InvalidOperationException(e.ToString()) ;
            }

            //if (pwd == System.Configuration.ConfigurationManager.AppSettings["ServicePwd"])
            //{
               
            //}


        }

        #region Edit

        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public String Edit(string module, int id)
        {
            if (IsPostedThisSite)
            {

                _serializer = new JavaScriptSerializer();
                var _sb = new StringBuilder();
                object _object;

                switch (module)
                {
                    case "forum":


                        // MainDB _db=new MainDB();

                        //// Forum _f = from p in _db.Forums where p.ForumId==id select p;



                        var _f = ForumsRepository.ForumDetail(id);

                        _object = new
                                      {

                                          _f.ForumId,
                                          _f.ForumName,
                                          _f.VisibleAtHomePage,
                                          _f.ModerateType,
                                          _f.RemoteUrl,
                                          _f.Keywords,
                                          _f.ParentId,
                                          _f.SortOrder,
                                          _f.Description,
                                          _f.AllowRoles,
                                          _f.AllowPostWithIn


                                      };
                        _serializer.Serialize(_object, _sb);

                        break;
                }

                return _sb.Replace("\"\\/Date(", "new Date(").Replace(")\\/\"", ").toUTCString()").ToString();



            }

            return null;

        }

        #endregion
    }
}
